from src.baseapp.controller.baseController import BaseController
from src.baseapp.model.testcaseServiceModel.projectModel import ProjectModel
from src.baseapp.model.BaseOrmOpeator import OrmOperator


class ProjectController(BaseController):
    def __init__(self):
        super().__init__()
        self.orm = OrmOperator(ProjectModel)

    def add_project(self, project_info: dict) -> tuple:
        """
        添加项目
        :param project_info: 项目信息
        :return: tuple (code, data)
        """
        response = self.response_data()
        try:
            project = ProjectModel(**project_info)
            self.orm.insert_data(project)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def delete_project(self, project_id: int) -> tuple:
        """
        删除项目
        :param project_id: 项目id
        :return: tuple (code, data)
        """
        response = self.response_data()
        try:
            project = self.orm.equal(ProjectModel.project_id, project_id).get_one()
            project.is_delete = 1
            self.orm.update_data(project)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def update_project(self, project_id: int, project_info: dict) -> tuple:
        """
        更新项目
        :param project_id: 项目id
        :param project_info: 项目信息
        :return: tuple (code, data)
        """
        response = self.response_data()
        try:
            project = self.orm.equal(ProjectModel.project_id, project_id).get_one()
            for key, value in project_info.items():
                setattr(project, key, value)
            self.orm.update_data(project)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def get_project_list(self, conditions: dict) -> tuple:
        """
        获取项目列表
        :param conditions:
        :return: tuple (code, data)
        """
        field_condition_mapping = {
            'project_id': ProjectModel.project_id,
            'project_name': ProjectModel.project_name,
            'create_user': ProjectModel.create_user_id,
            'create_time': ProjectModel.create_time,
            'Update_time': ProjectModel.update_time
        }
        response = self.response_data()
        try:
            self.orm = self.orm.equal(ProjectModel.is_delete, 0)
            for con_key, con_val in conditions.items():
                if "project_name" == con_key:
                    self.orm = self.orm.like(field_condition_mapping[con_key], con_val)
                elif "create_user" == con_key:
                    self.orm = self.orm.equal(field_condition_mapping[con_key], con_val)
                else:
                    self.orm = self.orm.between(field_condition_mapping[con_key], con_val[0], con_val[1])
            projects = self.orm.get_all()
            response.data['projects'] = [
                {
                    'project_id': project.project_id,
                    'project_name': project.project_name,
                    'create_user_id': project.create_user_id,
                    'create_time': project.create_time,
                    'Update_time': project.update_time
                }
                for project in projects
            ]
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def get_project_by_id(self, project_id: int) -> tuple:
        """
        根据项目id获取项目信息
        :param project_id: 项目id
        :return: tuple (code, data)
        """
        response = self.response_data()
        try:
            project: ProjectModel = self.orm.equal(ProjectModel.project_id, project_id).get_one()
            response.data['project'] = {
                'project_id': project.project_id,
                'project_name': project.project_name,
                'description': project.description,
                'create_user_id': project.create_user_id,
                'create_time': project.create_time,
                'Update_time': project.update_time
            }
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict
